var app =
    angular.module('app')
        .config(
        [
            '$controllerProvider', '$compileProvider', '$filterProvider', '$provide',
            function($controllerProvider, $compileProvider, $filterProvider, $provide) {
                app.controller = $controllerProvider.register;
                app.directive = $compileProvider.directive;
                app.filter = $filterProvider.register;
                app.factory = $provide.factory;
                app.service = $provide.service;
                app.constant = $provide.constant;
                app.value = $provide.value;

            }
        ]);

app.factory('TokenInterceptor', ['$q', '$window', '$location', 'userSession', function($q, $window, $location, userSession) {
    return {
        request: function (config) {
            config.headers = config.headers || {};
            if ($window.sessionStorage.getItem('SESSION')) {
                config.headers['x-auth-token'] = $window.sessionStorage.getItem('SESSION');
            }
            return config;
        },

        requestError: function(rejection) {
            return $q.reject(rejection);
        },

        /* Set Authentication.isAuthenticated to true if 200 received */
        response: function (response) {
            if (response !== null && response.status == 200) {

            }
            return response || $q.when(response);
        },

        /* Revoke client authentication if 401 is received */
        responseError: function(rejection) {
            //if (rejection !== null && rejection.status === 401 && userSession.user) {
            //    userSession.destroy();
            //    $window.sessionStorage.removeItem("userSession");
            //    $window.sessionStorage.removeItem("token");
            //    alert("用户超时，请重新登陆!");
            //    $location.path("login");
            //}
            return $q.reject(rejection);
        }
    };
}]);

app.config(function($breadcrumbProvider,$httpProvider) {
    $breadcrumbProvider.setOptions({
        template: '<ul class="breadcrumb"><li><i class="fa fa-home"></i><a href="#">首页</a></li><li ng-repeat="step in steps" ng-class="{active: $last}" ng-switch="$last || !!step.abstract"><a ng-switch-when="false" href="{{step.ncyBreadcrumbLink}}">{{step.ncyBreadcrumbLabel}}</a><span ng-switch-when="true">{{step.ncyBreadcrumbLabel}}</span></li></ul>'
    });
    // 加入权限控制
    $httpProvider.interceptors.push('TokenInterceptor');
}).config(["w5cValidatorProvider", function (w5cValidatorProvider) {

    // 全局配置
    w5cValidatorProvider.config({
        blurTrig   : false,
        showError  : true,
        removeError: true

    });

    w5cValidatorProvider.setRules({
        username      : {
            required      : "输入的用户名不能为空",
            pattern       : "请输入正确的手机号"
        },
        password      : {
            required : "密码不能为空",
            minlength: "密码长度不能小于{minlength}",
            maxlength: "密码长度不能大于{maxlength}"
        },
        passwordConfirm: {
            required: "重复密码不能为空",
            repeat  : "两次密码输入不一致"
        },
        number        : {
            required: "数字不能为空"
        },
        customizer    : {
            customizer: "自定义验证数字必须大于上面的数字"
        },
        dynamicName:{
            required: "动态Name不能为空"
        },
        dynamic       : {
            required: "动态元素不能为空"
        },
        shortName     :{
            required  : "公司简称不能为空",
            w5cuniquecheck: "公司简称已经存在，请重新输入"
        },
        licenseImageURL:{
            required   : "请上传营业执照"
        }
    });
}]);

app.constant('AUTH_EVENTS', {
    loginSuccess: 'auth-login-success',
    loginFailed: 'auth-login-failed',
    logoutSuccess: 'auth-logout-success',
    sessionTimeout: 'auth-session-timeout',
    notAuthenticated: 'auth-not-authenticated',
    notAuthorized: 'auth-not-authorized'
});